/**
 * Definition for singly-linked list. */
  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* cur1 = l1, * cur2 = l2;
        int flag = 0;
        ListNode* head = new ListNode(-1);
        ListNode* cur = head;
        while (cur1 || cur2)
        {
            int num1 = cur1 ? cur1->val : 0;
            int num2 = cur2 ? cur2->val : 0;
            int sum = num1 + num2 + flag;
            flag = sum / 10;
            sum = sum % 10;
            ListNode* node = new ListNode(sum);
            cur->next = node;
            cur = cur->next;
            if (cur1)cur1 = cur1->next;
            if (cur2)cur2 = cur2->next;
        }
        if (flag)cur->next = new ListNode(1);
        ListNode* ret = head->next;
        delete head;
        return ret;
    }
};